package controllers

import (
	beego "github.com/beego/beego/v2/server/web"
	"user01/models"
)

type UserController struct {
	beego.Controller
}

func (c *UserController) Login() {
	var err error
	username := c.GetString("username")
	password := c.GetString("password")
	if username == "" || password == "" {
		c.Data["json"] = map[string]interface{}{
			"code": 0,
			"msg":  "数据不能为空",
		}
		c.ServeJSON()
		return
	}
	user := models.User{

		Username: username,
		Password: password,
	}
	err = models.DB.Create(&user).Error
	if err != nil {
		c.Data["json"] = map[string]interface{}{
			"code": 0,
			"msg":  "添加失败",
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = map[string]interface{}{
			"code": 200,
			"msg":  "添加成功",
		}
		c.ServeJSON()
		return
	}

}
func (c *UserController) Cat() {
	var err error
	username := c.GetString("username")
	password := c.GetString("password")
	if username == "" || password == "" {
		c.Data["json"] = map[string]interface{}{
			"code": 0,
			"msg":  "数据不能为空",
		}
		c.ServeJSON()
		return
	}
	var user models.User

	if models.Find(&user); err != nil {
		c.Data["json"] = map[string]interface{}{
			"code": 0,
			"msg":  "用户不存在",
		}
		c.ServeJSON()
		return
	}

}
